//
//  BusSelectController.m
//  EagleEye
//
//  Created by 18_李明明 on 15-1-29.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "BusSelectController.h"
#import "DataBaseHandler.h"
#import "Bus.h"
#import "BusController.h"

@interface BusSelectController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) DataBaseHandler *dataBase;
@property (nonatomic, strong) NSMutableArray *lineArr;
@property (nonatomic, strong) NSMutableArray *changeArr;
@property (nonatomic, strong) NSMutableArray *platArr;
@property (nonatomic, strong) NSMutableArray *bigArr;
@property (nonatomic, strong) NSArray *sectionArr;
@end

@implementation BusSelectController
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.lineArr = [NSMutableArray array];
        self.changeArr = [NSMutableArray array];
        self.platArr = [NSMutableArray array];
        self.bigArr = [NSMutableArray array];
        self.sectionArr = [NSArray arrayWithObjects:@"线路", @"换乘", @"站台", nil];
        
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataBase = [DataBaseHandler shareInstance];
    [self.dataBase openDB];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 30) style:UITableViewStyleGrouped];
    [self.view addSubview:self.tableView];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.bigArr = [NSMutableArray arrayWithObjects:self.lineArr, self.changeArr, self.platArr, nil];
    
}
// tableview方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[self.bigArr objectAtIndex:section] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"0502"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"0502"];
    }
    Bus *bus = [[self.bigArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    if (indexPath.section == 0) {
        cell.textLabel.text = bus.line;
        cell.detailTextLabel.text = bus.city;
    }else if (indexPath.section == 1) {
        NSString *str = [NSString stringWithFormat:@"%@ --> %@", bus.from, bus.to];
        cell.textLabel.text = str;
        cell.detailTextLabel.text = bus.city;
    }else {
        cell.textLabel.text = bus.station;
        cell.detailTextLabel.text = bus.city;
    }
    return cell;
}
// section的个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}
// 点击cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BusController *busVC = [[BusController alloc] init];
    Bus *bus = [[self.bigArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    busVC.str = bus.city;
    if (indexPath.section == 0) {
        
        busVC.line = bus.line;
    }else if (indexPath.section == 1) {
        busVC.fromStation = bus.from;
        busVC.toStation = bus.to;
    }
    else {
        busVC.station = bus.station;
    }
    NSLog(@"%@, %@", bus.from, bus.to);
    [self.navigationController pushViewController:busVC animated:YES];
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 44;
}


// 在section上放button
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    if (section == 0) {
        [button setTitle:@"线路" forState:UIControlStateNormal];
    }else if (section == 1) {
        [button setTitle:@"换乘" forState:UIControlStateNormal];
    }else {
        [button setTitle:@"站台" forState:UIControlStateNormal];
    }
    [button addTarget:self action:@selector(targetButton:) forControlEvents:UIControlEventTouchUpInside];

    button.backgroundColor = [UIColor whiteColor];
    return button;
}
- (void)targetButton:(UIButton *)button
{
    if ([button.titleLabel.text isEqualToString:@"线路"]) {
        if (self.lineArr.count == 0) {
            
       self.lineArr = [self.dataBase selectBusOfLine];
//        NSLog(@"%@", self.lineArr);
        self.bigArr = [NSMutableArray arrayWithObjects:self.lineArr, self.changeArr, self.platArr, nil];
        }else {
            self.lineArr = [NSMutableArray array];
            self.bigArr = [NSMutableArray arrayWithObjects:self.lineArr, self.changeArr, self.platArr, nil];
        }
        [self.tableView reloadData];
    }else if ([button.titleLabel.text isEqualToString:@"换乘"]) {
        if (self.changeArr.count == 0) {
        self.changeArr = [self.dataBase selectBusOfChange];
        self.bigArr = [NSMutableArray arrayWithObjects:self.lineArr, self.changeArr, self.platArr, nil];
        }else {
            self.changeArr = [NSMutableArray array];
            self.bigArr = [NSMutableArray arrayWithObjects:self.lineArr, self.changeArr, self.platArr, nil];
        }
        [self.tableView reloadData];
    }else {
        if (self.platArr.count == 0) {
        self.platArr = [self.dataBase selectBusOfStation];
        self.bigArr = [NSMutableArray arrayWithObjects:self.lineArr, self.changeArr, self.platArr, nil];
        }else {
            self.platArr = [NSMutableArray array];
            self.bigArr = [NSMutableArray arrayWithObjects:self.lineArr, self.changeArr, self.platArr, nil];
        }
        [self.tableView reloadData];
    }
    NSLog(@"%@", button.titleLabel.text);
}
// 删除cell
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (indexPath.section == 0) {
        NSString *tempStr = [cell.textLabel.text stringByAppendingString:cell.detailTextLabel.text];
        [self.dataBase deleteBusOfLine:tempStr];
        [self.lineArr removeObjectAtIndex:indexPath.row];
        NSLog(@"%d", indexPath.row);
        self.bigArr = [NSMutableArray arrayWithObjects:self.lineArr, self.changeArr, self.platArr, nil];
        [self.tableView reloadData];
    }else if (indexPath.section == 1) {
        Bus *bus = [self.changeArr objectAtIndex:indexPath.row];
        NSString *tempStr = bus.combine2;
        [self.dataBase deleteBusOfFromAndTo:tempStr];
        [self.changeArr removeObjectAtIndex:indexPath.row];
        self.bigArr = [NSMutableArray arrayWithObjects:self.lineArr, self.changeArr, self.platArr, nil];
        [self.tableView reloadData];
    }else {
        Bus *bus = [self.platArr objectAtIndex:indexPath.row];
        NSString *tempStr = bus.combine3;
        [self.dataBase deleteBusOfStation:tempStr];
        [self.platArr removeObjectAtIndex:indexPath.row];
        self.bigArr = [NSMutableArray arrayWithObjects:self.lineArr, self.changeArr, self.platArr, nil];
        [self.tableView reloadData];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
